import Vue from 'vue'
import Router from 'vue-router'
import IndexPage from '../pages/index'
import ProjectCreate from '../pages/project-create'
import Login from '../pages/login'

Vue.use(Router);
import Token from '../libs/token'

const router = new Router({
  mode: 'history',
  routes: [
    {
      path:'/login',
      name: 'login',
      component: Login,
    },
    {
      path: '/',
      name: 'index',
      component: IndexPage
    },
    {
      path:'/project/create',
      name:'create-project',
      component: ProjectCreate
    },
  ]
});

router.beforeEach((to, from ,next) => {
  let ignoreList = ['login'];
  if(ignoreList.indexOf(to.name) >= 0) {
    return next();
  }

  if(!Token.isValid()) {
    return next({
      name: 'login',
      query: {
        from: to.path
      }
    });
  }

  next();

});

export default router
